const { ccclass, property } = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {
    @property(cc.Node)
    player: cc.Node = null;

    @property([cc.Prefab])
    block_prefab: cc.Prefab[] = [];

    @property(cc.Node)
    block_root: cc.Node = null;

    @property(cc.Vec2)
    left_org: cc.Vec2 = cc.v2(0, 0);

    @property(cc.Node)
    map_root: cc.Node = null;

    y_radio:number= 0.5;

    @property(cc.Node)
    checkout: cc.Node = null;

    // @property
    // scoreFXMgr: 'ScoreFXMgr' = null;

    // @property
    // scoreOvertakeMgr: 'ScoreOvertakeMgr' = null;

    @property(cc.Label)
    scoreLabel: cc.Label = null;

    @property(cc.Label)
    hintControlLabel: cc.Label = null;

    @property(cc.Node)
    btnShare: cc.Node = null;

    // @property
    // screenLeaderboard:'screenLeaderboard'

    @property(cc.Sprite)
    playerSkinSprite: cc.Sprite = null;

    static instance: GameManager = null;

    mapMoving: boolean = false;
    isGameOver: boolean = true;
    gaming: boolean = false;
    mapRootInitPos: cc.Vec3 = cc.v3();
    score = 0;
    player_com = null;
    playerArray = null;
    currentSkinIndex = 0;

    // LIFE-CYCLE CALLBACKS:    

    onLoad() {
        GameManager.instance = this;
        this.player_com = this.player.getComponent("Player");
        this.mapRootInitPos = this.map_root.position;
    }

    start() {
        this.onGameStart(false);

    }

    cur_block: cc.Node = null;
    next_block: cc.Node = null;
    block_zorder: number

    //游戏开始
    onGameStart(showLabelScore) {
        this.isGameOver = false;
        this.map_root.position = this.mapRootInitPos;
        this.block_root.removeAllChildren(true);

        this.updateScoreDisplay();
        this.checkout.active = false;
        this.player_com.direction = 1;

        this.cur_block = cc.instantiate(this.block_prefab[Math.floor(Math.random() * 3)]);
        this.cur_block.position = this.block_root.convertToNodeSpaceAR(cc.v3(this.left_org));
        this.block_root.addChild(this.cur_block);

        //init player position
        let w_pos = this.cur_block.getChildByName("mid").convertToWorldSpaceAR(cc.v3(0, 0));
        this.player.position = cc.v3(this.map_root.convertToNodeSpaceAR(w_pos));
        this.next_block = this.cur_block;
        this.block_zorder = -1;

        this.add_block();
        this.showLabelScore(showLabelScore);

        // UtilsFB.invitePlayerRandomAsync(UtilsCommon.getScreenshotBase64(UtilsCommon.getCameraMain())).catch(error => {});
    }

    showLabelScore(active: boolean) {
        this.scoreLabel.node.active = active;
        this.hintControlLabel.node.active = active;
        this.btnShare.active = active;
    }


    add_block() {
        this.cur_block = this.next_block;

        this.next_block = cc.instantiate(this.block_prefab[Math.random() * 3 | 0]);
        this.block_root.addChild(this.next_block);

        //this.next_block.zIndex = this.block_zorder;
        //this.block_zorder--;

        //计算下一个解答的x坐标和y坐标距离
        let x_distance = 200 + Math.random() * 200 | 0;
        let y_distance = x_distance * this.y_radio;

        let next_pos = this.cur_block.position;
        next_pos.x += (x_distance * this.player_com.direction);
        next_pos.y += y_distance;
        this.next_block.position = cc.v3(next_pos);
        cc.log(x_distance + ' ' + y_distance);

        this.player_com.set_next_block(this.next_block.getComponent("Block"));

        // 删除掉没有用的block
        for (let i = this.block_root.childrenCount - 1; i >= 0; i--) {
            this.destroyOutBlock(this.block_root.children[i])
        }
        // end 
    }

    // update (dt) {}
    destroyOutBlock(blockNode) {
        let block = blockNode.getComponent('block');
        let blockPosWorld = blockNode.parent.convertToWorldSpaceAR(blockNode.getPosition());
        let gamePosWorld = this.node.getPosition();
        let distance = blockPosWorld.sub(gamePosWorld);
        let minDistanceX = this.node.width / 2 + blockNode.width / 2;
        let minDistanceY = this.node.height / 2 + blockNode.height / 2;

        if (Math.abs(distance.x) > minDistanceX && Math.abs(distance.y) > minDistanceY) {
            blockNode.destroy();
        }
    }

    move_map(offset_x, offset_y) {
        this.mapMoving = true;
        var m1 = cc.moveBy(0.5, offset_x, offset_y);
        var end_func = cc.callFunc(function () {
            this.mapMoving = false;
            this.add_block();
        }.bind(this));

        var seq = cc.sequence([m1, end_func]);
        this.map_root.runAction(seq);
    }

    on_checkout_game() {
        this.isGameOver = true;
        this.showLabelScore(false);
        // ScreenMgr.instance.showScreen('ScreenGameOver');
        return;
    }

    gainScore(posWorld, score: number) {
        this.score += score;
        this.updateScoreDisplay();
        //this.scoreFXMgr.playScoreFX(posWorld, score);
        // let playerOvertaken = UtilsFB.getPlayerInfoScoreOvertake(leaderboardScore, this.score - score, this.score);
        // if (playerOvertaken.length > 0) {
        //     this.scoreOvertakeMgr.play(posWorld, playerOvertaken);
        // }
    }

    // 显示文字
    updateScoreDisplay() {
        this.scoreLabel.string = "Score: " + this.score;
    }

    //TODO 显示排行榜
    onShowLeaderboard() {
        //this.screenLeaderboard.showLeaderboard(leaderboardScore);
    }

    //TODO 改变皮肤
    onSkinChange(skinIndex) {

    }


}
